<template>
  <div class="form-container">
    <el-form inline :model="form" ref="refForm" class="form-line" label-width="70">
      <el-form-item label="卡卷名称" prop="couponName">
        <el-input size="default" v-model="form.couponName" placeholder="卡卷名称"></el-input>
      </el-form-item>
      <el-form-item label="卡卷类型" prop="couponType">
        <el-select v-model="form.couponType" placeholder="卡券类型">
          <el-option label="配送券" value="1"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div class="btn-container">
      <el-button type="primary" @click="handleReset(refForm as FormInstance)">重置</el-button>
      <el-button type="primary" @click="getList">查询</el-button>
      <el-button type="primary" @click="router.push('/cityLevel/cityMarket/coupons/edit')">添加卡券</el-button>
    </div>
  </div>
  <div class="table-container">
    <el-table
      border
      :cell-style="{ textAlign: 'center', fontSize: '13px' }"
      :header-cell-style="{
        textAlign: 'center',
        fontWeight: '100',
        fontSize: '13px',
        color: 'white',
        backgroundColor: '#009688'
      }"
      :data="list"
      style="width: 100%"
    >
      <el-table-column prop="couponName" label="卡券名称" />
      <el-table-column prop="couponType" label="卡券类型">
        <template #default="scope">{{ scope.row.couponType == 1 ? "配送券" : "" }}</template>
      </el-table-column>
      <el-table-column prop="couponPrice" label="卡券原价" />
      <el-table-column prop="couponDiscount" label="卡券折扣价" />
      <el-table-column prop="useTimes" label="可使用次数" />
      <el-table-column prop="expire" label="有效时长" />
      <el-table-column prop="status" label="状态">
        <template #default="scope">
          <el-switch v-model="scope.row.status" disabled active-value="1" inactive-value="0"></el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="添加时间" />
      <el-table-column fixed="right" label="操作" width="250">
        <template #default="scope">
          <el-button :disabled="scope.row.status != '0'" link type="primary" size="small" @click="handleDetele(scope.row)">
            删除
          </el-button>
          <el-button link type="primary" size="small" @click="handleEdit(scope.row.couponId)"> 修改 </el-button>
          <el-button link type="primary" size="small" @click="handleDetail(scope.row.couponId)"> 详情 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        size="small"
        v-model:current-page="form.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        layout="prev, pager, next"
        :total="pageTotal"
        @change="getList"
        :page-size="form.pageSize"
      />
    </div>
  </div>
</template>

<script lang="tsx" name="CitySiteIndex" setup>
import { Coupon } from "@/api/interface/city/cityMarket/coupon";
import { deleteCouponAPI, getCouponListAPI } from "@/api/modules/city/cityMarket/coupon/api";
import { ElMessage, ElMessageBox, FormInstance } from "element-plus";
import { onMounted, ref } from "vue";
import { useRouter } from "vue-router";
const router = useRouter();
let pageTotal = ref<number>(0);
let form = ref<Coupon.SearchParams>({
  province: JSON.parse(localStorage.getItem("userInfo") as string).province,
  city: JSON.parse(localStorage.getItem("userInfo") as string).city,
  couponName: "",
  pageNum: 1,
  pageSize: 10
});
const refForm = ref<FormInstance>();
const list = ref<Coupon.Row[]>([]);
const handleReset = (ruleForm: FormInstance) => {
  ruleForm.resetFields();
};
const handleEdit = (id: string) => {
  router.push({ path: "/cityLevel/cityMarket/coupons/edit", query: { id } });
};
const handleDetail = (id: string) => {
  router.push({ path: "/cityLevel/cityMarket/coupons/detail", query: { id } });
};
const getList = async () => {
  const { rows, code, msg, total } = await getCouponListAPI(form.value);
  if (code == "200" && rows) {
    list.value = rows;
    if (total) {
      pageTotal.value = total;
    }
  } else {
    ElMessage.error(msg);
  }
};
// 删除
const handleDetele = (row: Coupon.Row) => {
  ElMessageBox.confirm(`此操作将永久删除卡券【${row.couponName}】，是否继续？`)
    .then(() => {
      dele(row.id as string);
    })
    .catch(() => {
      ElMessage.info("取消删除");
    });
};
const dele = async (id: string) => {
  const { code, msg } = await deleteCouponAPI(id);
  if (code == "200") {
    ElMessage.success("删除成功");
  } else {
    ElMessage.error(msg);
  }
};
onMounted(() => {
  getList();
});
</script>
<style lang="scss" scoped>
.form-container {
  padding: 10px;
  margin-bottom: 10px;
  background-color: white;
  .btn-container {
    display: inline-block;
    float: right;
    vertical-align: middle;
  }
  .el-form {
    display: inline-block;
  }
  .form-line {
    .el-form-item {
      margin: 0;
      margin-right: 20px;
    }
    .el-input {
      width: 240px;
    }
    .el-select {
      width: 240px;
    }
  }
}
.table-container {
  padding: 40px 10px;
  padding-bottom: 60px;
  background-color: white;
  .pagination-container {
    // display: inline-block;
    float: right;
    margin-top: 20px;
    background-color: white;
  }
}
::v-deep .el-form-item__label {
  font-size: 13px;
}
::v-deep .el-button > span {
  font-size: 13px;
}
::v-deep .el-input__inner {
  font-size: 13px;
}
</style>
